package com.zaofan.service;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zaofan.bean.Account;
import com.zaofan.bean.UserFollow;
import com.zaofan.dao.UserFollowDao;
import com.zaofan.exception.ServiceException;
import com.zaofan.utils.GsonUtils;
import com.zaofan.utils.HTTPClient;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UserFollowService {
	private UserFollowDao userFollowDao = new UserFollowDao();
	private AccountService accountService = new AccountService();

	public void delete(UserFollow userFollow) {
		userFollowDao.del(userFollow);
	}

	public void add(UserFollow userFollow) {
		userFollowDao.add(userFollow);
	}

	public void update(UserFollow userFollow) {
		userFollowDao.update(userFollow);
	}

	public List<UserFollow> findAll(Map<String, Object> params) {
		List<UserFollow> data = userFollowDao.findAll(params);
		if (data == null) {
			data = new ArrayList<>();
		}
		return data;
	}

	public Long count(Map<String, Object> params) {
		return userFollowDao.count(params);
	}

	public UserFollow findById(Integer id) {
		Map<String, Object> params = new HashMap<>();
		params.put("id", id);
		return userFollowDao.findOne(params);
	}

	public void followUser(Integer userId, Integer otherId) throws ServiceException {
		Account user = accountService.findById(userId);
		if (user == null) {
			throw new ServiceException("对不起,您未登录,请重新登录试试!!!");
		}
		Account other = accountService.findById(otherId);
		if (other == null) {
			throw new ServiceException("参数错误!!!");
		}
		UserFollow follow = new UserFollow();
		follow.setAddTime(new Date());
		follow.setOtherId(other.getId());
		follow.setUserId(user.getId());
		this.add(follow);
	}

	public boolean isFollow(int userId, int otherId) {
		Map<String, Object> params = new HashMap<>();
		params.put("userId", userId);
		params.put("otherId", otherId);
		if (userFollowDao.findOne(params) == null) {
			return false;
		} else {
			return true;
		}
	}

	public List<com.zaofan.mobile.bean.UserFollow> getUserFans(Integer userId, String begin, String end) {
		return userFollowDao.getUserFans(userId, begin, end);
	}

	public List<com.zaofan.mobile.bean.UserFollow> getUserFollow(Integer userId, String begin, String end) {
		return userFollowDao.getUserFollow(userId, begin, end);
	}

	public void userCancelFollow(int userId, Integer otherId) throws ServiceException {
		Map<String, Object> params = new HashMap<>();
		params.put("userId", userId);
		params.put("otherId", otherId);
		UserFollow follow = userFollowDao.findOne(params);
		if(follow == null){
			throw new ServiceException("对不起,您还未关注改用户!!!");
		}
		this.delete(follow);
	}

	public String getOpenId(HttpServletRequest request,String redirect_url,HttpServletResponse response) throws IOException {
		String code  =  request.getParameter("code");
		if(code == null || code.equals("")){
			String http = "https://open.weixin.qq.com/connect/oauth2/authorize?";
			String appid = "&appid=wxf946190506655c2d";
			String redirect_uri = "&redirect_uri=" +
					URLEncoder.encode(redirect_url, "utf8");
			String response_type = "&response_type=code";
			String scope = "&scope=snsapi_base";
			String state = "&state=16516516156#wechat_redirect";
			response.sendRedirect(http + appid + redirect_uri + response_type + scope + state);
			return "";
//			return "redirect:"+http + appid + redirect_uri + response_type + scope + state;
		}
		System.out.println("--------code-------"+code+"----------");
		String url = "https://api.weixin.qq.com/sns/oauth2/access_token" +
				"?appid=wxf946190506655c2d"+
				"&secret=25f4e517eeea25ea18e75cab31cc3b8b" +
				"&grant_type=authorization_code" +
				"&code=" + code;

		String openid = "";

		String result = HTTPClient.postJson(url, "");
		if (StringUtils.isNoneEmpty(result)) {
			HashMap map = GsonUtils.fromJson(result, HashMap.class);
			 openid = map.get("openid").toString();
		}
		return openid;
	}

}
